package com.carrot.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.carrot.common.core.domain.dto.SysPointDto;
import com.carrot.common.core.domain.entity.SysPoint;
import com.carrot.common.core.domain.entity.SysPointHistoryMap;
import com.carrot.common.core.domain.entity.SysPointStandardField;
import com.carrot.common.exception.ServiceException;
import com.carrot.common.mybatis.BaseServiceImpl;
import com.carrot.common.utils.StringUtils;
import com.carrot.system.mapper.SysPointMapper;
import com.carrot.system.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 场站 业务层处理
 *
 * @author liuhongzhen
 */
@Service
public class SysPointServiceImpl extends BaseServiceImpl<SysPointMapper, SysPoint>
        implements ISysPointService {

    private static final Logger log = LoggerFactory.getLogger(SysPointServiceImpl.class);

    @Autowired
    private ISysPointHistoryMapService historyMapService;
    @Autowired
    private ISysPointStandardFieldService standardFieldService;

    @Transactional(rollbackFor = ServiceException.class)
    @Override
    public void changeStatus(SysPointDto dto) {
        UpdateWrapper<SysPoint> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("id", dto.getId());
        updateWrapper.set("online_enable", dto.getOnlineEnable());
        this.update(updateWrapper);
    }

    @Transactional(rollbackFor = ServiceException.class)
    @Override
    public void matchMapField(Long id) {
        SysPoint point = this.getById(id);
        // 更新点表数据
        String equipmentType = point.getEquipmentType().replaceAll(" ", "");
        String description = point.getDescription().replaceAll(" ", "");

        QueryWrapper<SysPointHistoryMap> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("equipment_type", equipmentType);
        queryWrapper1.eq("description", description);

        List<SysPointHistoryMap> historyMaps = historyMapService.list(queryWrapper1);
        if (historyMaps.isEmpty()) {
            point.setMapField("");
            point.setMatchType("0");
        } else {
            Set<String> mapFields =
                    historyMaps.stream()
                            .map(SysPointHistoryMap::getMapField)
                            .filter(StringUtils::isNotEmpty)
                            .collect(Collectors.toSet());

            String mapField = String.join(",", mapFields);
            point.setMapField(mapField);
            String matchType = mapFields.size() > 1 ? "2" : "1";
            point.setMatchType(matchType);
        }
        // 更新
        this.updateById(point);
    }

    @Transactional(rollbackFor = ServiceException.class)
    @Override
    public void matchStandardField(Long id) {

        SysPoint point = this.getById(id);

        String equipmentType = point.getEquipmentType();
        String mapField = point.getMapField();

        QueryWrapper<SysPointStandardField> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("equipment_type", equipmentType);
        queryWrapper1.eq("map_field", mapField);
        queryWrapper1.last("limit 1");

        SysPointStandardField standardField = standardFieldService.getOne(queryWrapper1);

        if (null == standardField) {
            point.setMatchType("0");
        } else {
            point.setPropertyName(standardField.getPropertyName());
            point.setRemark(standardField.getRemark());
            point.setResultType(standardField.getResultType());
        }
        // 更新
        this.updateById(point);
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void batchUpdateEquipmentId(String stationId, String equipmentType,
                                       String equipmentId, String newEquipmentId) {

        UpdateWrapper<SysPoint> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("station_id", stationId);
        updateWrapper.eq("equipment_type", equipmentType);
        updateWrapper.eq("equipment_id", equipmentId);
        updateWrapper.set("equipment_id", newEquipmentId);

        this.update(updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void batchDelEquipmentId(String stationId, String equipmentType, String equipmentId) {

        UpdateWrapper<SysPoint> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("station_id", stationId);
        updateWrapper.eq("equipment_type", equipmentType);
        updateWrapper.eq("equipment_id", equipmentId);

        this.remove(updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void del(Long id) {
        this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void batchDel(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void delByStation(String stationId) {

        UpdateWrapper<SysPoint> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("station_id", stationId);
        this.remove(updateWrapper);
    }
}
